package com.devicemantra.coordinate.interfaces.http;

import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import com.devicemantra.coordinate.exceptions.operations.CReadPayloadException;


public class CHttpReader 
{
	public static String getAsString(HttpServletRequest pHttpServletRequest) throws CReadPayloadException
	{
		/* First let us get a BufferedReader from the request object */
		BufferedReader lReqBufferedReader = null;
		try {
			lReqBufferedReader = pHttpServletRequest.getReader();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new CReadPayloadException("Error while pHttpServletRequest.getReader();");
		}
		
		/* Lets create a char bufffer of 2kb to read the incoming message */
		char[] lInputBuffer = new char[2048];
		/* Read till we reach the end of the stream */
		int lNumBytesRead = 0;
		StringBuffer lStringBuffer = new StringBuffer();
		try {
			while((lNumBytesRead = lReqBufferedReader.read(lInputBuffer)) != -1 )
			{
				lStringBuffer.append(lInputBuffer,0,lNumBytesRead);
			}
		}
		catch(IOException e) {
				e.printStackTrace();
				throw new CReadPayloadException("Error reading payload");
		}
		return lStringBuffer.toString();
	}
	
	
	
}
